#!/usr/bin/python

'''Base task for sqlite3
'''

import sqlite3
import logging
from db.sqllite3_wrapper import DbAccess
from basetask import BaseTask

class Sqlite3Task(BaseTask):
    '''Base task for DB
    '''

    def __init__(self, db):
        super().__init__("sqlite3_task")
        self._db = db
        self.__logger = logging.getLogger(__name__)

    def process(self, context):
        self._db.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
        self.__logger.info("task '%s': table created", self.name)
        ##insert
        self._db.executescript("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\
      VALUES (1, 'Paul', 32, 'California', 20000.00 );\
      INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)\
      VALUES (2, 'Allen', 25, 'Texas', 15000.00 );")
        self._db.executemany("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)",
                              [(3, 'Teddy', 23, 'Norway', 20000.00),
                               (4, 'Mark', 25, 'Rich-Mond ', 65000.00)])
        self.__logger.info("task '%s': data inserted", self.name)
        ##fetch all
        allResult = self._db.fetchall("SELECT id, name, address, salary  from COMPANY;")
        self.__logger.info("task '%s': drop table", self.name)
        #self._db.execute("DROP TABLE COMPANY;")
        return allResult

    def verify(self, context):
        return True


    